package com.hxl.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.hxl.entity.CmfzUser;
import com.hxl.mapper.CmfzUserMapper;
import com.hxl.service.CmfzUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxl.util.ThreadPollUtil;
import org.apache.poi.ss.formula.functions.Count;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 * InnoDB free: 10240 kB 服务实现类
 * </p>
 *
 * @author hxl
 * @since 2020-08-18
 */
@Service
public class CmfzUserServiceImpl extends ServiceImpl<CmfzUserMapper, CmfzUser> implements CmfzUserService {
    @Autowired
    private CmfzUserMapper cmfzUserMapper;

    //创建日志对象
    private Logger logger = LoggerFactory.getLogger(CmfzUserServiceImpl.class);
    @Override
    public List<Map> selectCountByGender() {
        return cmfzUserMapper.selectCountByGender();
    }

    @Override
    public List<Map> selectCountByWeeks() {
        return cmfzUserMapper.selectCountByWeeks();
    }

    @Override
    public List<Map> selectCountByMap() {
        return cmfzUserMapper.selectCountByMap();
    }

    @Override
    public Map testMulThread() {
        Map map = new HashMap();

        //创建线程计数器
        CountDownLatch count = new CountDownLatch(3);

        //创建线程池
        ThreadPoolExecutor executor = ThreadPollUtil.executor;

        logger.info("开启多线程");

        //创建子线程执行子任务
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("sex",selectCountByGender());
                //计数器减一
                count.countDown();
            }
        });

        executor.submit(()->{
            map.put("week",selectCountByWeeks());
            //计数器减一
            count.countDown();
        });

        executor.submit(()->{
            map.put("map",selectCountByMap());
        });
        logger.error("多线程结束");
        return map;
    }
}
